package com.lagou.dao.impl;

import com.lagou.dao.CourseDao;
import com.lagou.pojo.Course;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {


    public List<Course> findCourseList() {
        // 1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        // 2.编写sql,判断是否删除 取出is_del = 0 的数据，未删除的数据
        String sql = "select id,course_name,price,sort_num,`status`\n" +
                "from course\n" +
                "where is_del= ?";
        // 3.执行查询
        try {
            List<Course> courseList = qr.query(sql, new BeanListHandler<Course>(Course.class), 0);
            return courseList;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    // 根据条件查询课程信息
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        // 编写sql，当前查询为多条件 不定项查询
        StringBuffer sb = new StringBuffer("select id,course_name,price,sort_num,`status`\n" +
                "FROM course WHERE 1=1 and is_del = ? ");
        // 创建list集合 保存参数
        List<Object> list = new ArrayList<Object>();
        list.add(0);
        // 判断传进来的参数是否为空
        if(courseName!=null&&courseName!=""){
            sb.append(" AND course_name like ? ");
            courseName = "%"+courseName+"%";
            list.add(courseName);
        }
        if (status!=null&&status!=""){
            sb.append(" AND `status` = ? ");
            int i = Integer.parseInt(status);
            list.add(i);
        }
        // 执行查询
        try {
            List<Course> query = qr.query(sb.toString(), new BeanListHandler<Course>(Course.class), list.toArray());
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //保存课程营销信息

    public int saveCourseSalesInfo(Course course) {

        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            //2.编写SQL
            String sql = "INSERT INTO course(\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "course_img_url,\n" +
                    "STATUS,\n" +
                    "create_time,\n" +
                    "update_time\n" +
                    ")VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            //3.准备参数
            Object[] param = {course.getCourse_name(), course.getBrief(), course.getTeacher_name(), course.getTeacher_info(),
                    course.getPreview_first_field(), course.getPreview_second_field(), course.getDiscounts(), course.getPrice(),
                    course.getPrice_tag(), course.getShare_image_title(), course.getShare_title(), course.getShare_description(),
                    course.getCourse_description(), course.getCourse_img_url(), course.getStatus(), course.getCreate_time(), course.getUpdate_time()};

            //4.执行插入操作
            int row = qr.update(sql, param);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course findCourseById(int id) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT \n" +
                    "id,\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "course_img_url,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "STATUS\n" +
                    "FROM course WHERE id = ?;";
            Course query = qr.query(sql, new BeanHandler<Course>(Course.class), id);
            return  query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateCourseSalesInfo(Course course) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "UPDATE course SET \n" +
                "course_name = ?,\n" +
                "brief = ?,\n" +
                "teacher_name = ?,\n" +
                "teacher_info = ?,\n" +
                "preview_first_field = ?,\n" +
                "preview_second_field = ?,\n" +
                "discounts = ?,\n" +
                "price = ?,\n" +
                "price_tag = ?,\n" +
                "share_image_title = ?,\n" +
                "share_title = ?,\n" +
                "share_description = ?,\n" +
                "course_description = ?,\n" +
                "course_img_url = ?,\n" +
                "update_time = ? \n" +
                "WHERE id = ?";

        Object[] param = {course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info(),
                course.getPreview_first_field(),course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),
                course.getPrice_tag(),course.getShare_image_title(),course.getShare_title(),course.getShare_description(),course.getCourse_description(),
                course.getCourse_img_url(),course.getUpdate_time(),course.getId()};

        try {
            int row = qr.update(sql, param);
            return row;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }


    }

    @Override
    public int updateCourseStatus(Course course) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course set status = ?,update_time = ? where id = ?";
            String dateFormart = DateUtils.getDateFormart();
            Object []  params = {course.getStatus(),dateFormart,course.getId()};
            int update = qr.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }
}
